cmake_minimum_required(VERSION 3.5)

project(assgl)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# # 添加一个自定义的清理目标
# set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_COMMAND} -E echo Compiling ...")
# set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CMAKE_COMMAND} -E echo Linking ...")

# set(CMAKE_BUILD_TYPE Debug)
# if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
#     set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../libPath/libassgl)
# endif()

find_package(PkgConfig REQUIRED)
find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)
find_package(harfbuzz REQUIRED)
# pkg_check_modules(FFMPEGLIB REQUIRED IMPORTED_TARGET libavutil )
# message(STATUS "FFMPEGLIB_INCLUDE:${FFMPEGLIB_INCLUDE_DIRS}")
include(Utils.cmake)
pkgCheckModulesFun(FFMPEGLIB "libavutil libswscale")
pkgCheckModulesFun(OPENCVLIB "opencv4")

find_library(glTextEffect libglTextEffect.a ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/lib)

include_directories(
    /usr/include/freetype2
	${CMAKE_CURRENT_SOURCE_DIR}
    ${FFMPEGLIB_INCLUDE_DIRS}
    ${OPENCVLIB_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include
)

message(STATUS "PROJECT_NAME:${PROJECT_NAME}")

file(GLOB  project_file 
    # ${CMAKE_CURRENT_SOURCE_DIR}/glad.c 
    # ${CMAKE_CURRENT_SOURCE_DIR}/Shader.cpp
	${CMAKE_CURRENT_SOURCE_DIR}/assgl.cpp
	# ${CMAKE_CURRENT_SOURCE_DIR}/assglOpengl.cpp
	${CMAKE_CURRENT_SOURCE_DIR}/assglParser.cpp
	${CMAKE_CURRENT_SOURCE_DIR}/assglUtils.cpp 
    ${CMAKE_CURRENT_SOURCE_DIR}/assglConvertMid.cpp 
	)

message(STATUS "CMAKE_CURRENT_SOURCE_DIR:${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "src:${project_file}")


if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
add_library(${PROJECT_NAME}-static STATIC  $<TARGET_OBJECTS:${PROJECT_NAME}>)
set_target_properties(${PROJECT_NAME}-static PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME}-static
    # -Wl,--start-group
    pthread m dl
    GL glfw glut GLEW::GLEW
    
    Xrandr
    X11

    # icuuc
    harfbuzz
    
    freetype freeimage
    ${FFMPEGLIB_LIBRARIES}
    ${OPENCVLIB_LIBRARIES}
    -Wl,--whole-archive
    ${glTextEffect}
    -Wl,--no-whole-archive

    # -Wl,--end-group
    )

# add_library(${PROJECT_NAME}-shared SHARED  $<TARGET_OBJECTS:${PROJECT_NAME}>)
# set_target_properties(${PROJECT_NAME}-shared PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
# else()

endif()

add_library(${PROJECT_NAME} SHARED  ${project_file})
target_link_libraries(${PROJECT_NAME}
    # -Wl,--start-group
    pthread m dl
    GL glfw glut GLEW::GLEW
    
    Xrandr
    X11

    # icuuc
    harfbuzz
    
    freetype freeimage
    ${FFMPEGLIB_LIBRARIES}
    ${OPENCVLIB_LIBRARIES}
    -Wl,--whole-archive
    ${glTextEffect}
    -Wl,--no-whole-archive

    # -Wl,--end-group
    )

message(STATUS "CMAKE_INSTALL_PREFIX:${CMAKE_INSTALL_PREFIX}" )
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(FILES 
    ${CMAKE_CURRENT_SOURCE_DIR}/assgl.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/gltxt.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/glcontext.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/overlayobj.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/stb_image.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/stb_image_write.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/comm.h
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/glesdef.h
    DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
install(DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/EGL
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/glad
    ${CMAKE_CURRENT_SOURCE_DIR}/core/gltxt/include/KHR 
    DESTINATION ${CMAKE_INSTALL_PREFIX}/include)    
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/../libassgl.pc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig)

# 添加一个自定义的清理命令
add_custom_target(uninstall
    COMMAND echo "uninstall ..."
    COMMAND make clean
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/lib${PROJECT_NAME}.a
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/lib${PROJECT_NAME}.so
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/assgl.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/gltxt.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/glcontext.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/overlayobj.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/stb_image.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/stb_image_write.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/comm.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/glesdef.h
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/EGL
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/glad
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/include/KHR 
    BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig/libassgl.pc
    )

